#include "form.h"
#include "ui_form.h"
#include "regist.h"
#include "mainwindow.h"
#include "user.h"

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);
    //取消顶部功能框（关闭界面按钮）
    this->setWindowFlags(Qt::FramelessWindowHint);
    //设置右侧label_picture的背景图片
    QPixmap *pix = new QPixmap(":/new/prefix1/pct/regist_picture.jpg");
    QSize sz = ui->label_picture->size();
    ui->label_picture->setPixmap(pix->scaled(sz));

    //设置图片阴影效果

    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setOffset(-3, 0);
    shadow->setColor(QColor("#888888"));
    shadow->setBlurRadius(30);
    ui->label_head->setGraphicsEffect(shadow);
    ui->label_picture->setGraphicsEffect(shadow);

}

Form::~Form()
{
    delete ui;
}

//void Form::paintEvent(QPaintEvent *event)
//{
//    QPainter painter(this);

//    painter.drawPixmap(0,0,width(),height(),QPixmap(":/pct/d4f979a6c21beabf313eda783269afe5.jpg"));
//}

void Form::on_pushButton_close_clicked()
{
    close();
}

void Form::on_pushButton_2_clicked()
{
    this->close();
    Regist *s = new Regist;
    s->show();
}

void sqlite_Init()
{

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("user.db");
    if(!db.open())
    {
        qDebug()<<"open error";
    }
    //create excle
    QString createsql=QString("create table if not exists user(id integer primary key autoincrement,"
                        "username ntext unique not NULL,"
                        "password ntext not NULL)");
    QSqlQuery query;
    if(!query.exec(createsql)){
        qDebug()<<"table create error";
    }
    else{
        qDebug()<<"table create success";
    }
}

void Form::on_pushButton_clicked()
{
    sqlite_Init();
        QString username = ui->lineEdit_username->text();
        QString password = ui->lineEdit_passwd->text();
        QString sql=QString("select * from user where username='%1' and password='%2'")
                .arg(username).arg(password);
        //创建执行语句对象
        QSqlQuery query(sql);
        //判断执行结果
        if(!query.next())
        {
            qDebug()<<"Login error";
            QMessageBox::information(this,"登录认证","登录失败,账户或者密码错误");
        }
        else
        {
            qDebug()<<"Login success";
//            QMessageBox::information(this,"登录认证","登录成功");
            //登录成功后可以跳转到其他页面
            MainWindow *w = new MainWindow;
            w->show();
            this->close();
        }
}
